package main

import (
	"context"
	"fmt"
	"github.com/apache/rocketmq-client-go/v2"
	"github.com/apache/rocketmq-client-go/v2/primitive"
	"github.com/apache/rocketmq-client-go/v2/producer"
)

func main() {
	p, _ := rocketmq.NewProducer(
		producer.WithNsResolver(primitive.NewPassthroughResolver([]string{"127.0.0.1:9876"})),
		producer.WithRetry(2),
		producer.WithGroupName("TagGroup"), // 定义生产者组
	)
	err := p.Start()
	if err != nil {
		fmt.Println("start producer error:", err)
		return
	}
	defer p.Shutdown()

	topic := "TagTopic"
	for i := 300; i < 1000; i++ {
		ms := fmt.Sprintf("Hello RocketMQ ---- %d", i)
		msg := &primitive.Message{
			Topic: topic,
			Body:  []byte(ms),
		}
		if i%2 == 0 {
			msg.WithTag("TagA")
		} else {
			msg.WithTag("TagB")
		}
		res, err := p.SendSync(context.Background(), msg)

		if err != nil {
			fmt.Println("send message error:", err)
		} else {
			fmt.Printf("send message success: result=%s\n", res.String())
		}
	}

}
